package com.alm.token.ui.activity;

import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.alm.token.ConStant;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.bean.GoogleAuthKeyData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.UpdateData;
import com.alm.token.ui.data.bean.UserData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.MainView;
import com.alm.token.utils.ADBInstallApk;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.utils.LogUtil;
import com.alm.token.utils.SpUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;

import baibeiwallet.WalletDaoUtils;
import baibeiwallet.WalletManager;
import baibeiwallet.WalletManagerActivity1;
import baibeiwallet.event.RefreshMeEvent;
import baibeiwallet.utils.DialogUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.alm.token.ui.activity.BindEmaiActivity.BINDMAI_up;
import static com.alm.token.ui.activity.BindGoogleCodeActivity.BINDGOOGLECODE_BINd;
import static com.alm.token.ui.activity.BindGoogleCodeActivity.BINDGOOGLECODE_UNBINd;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_INTO_CODE;
import static com.alm.token.ui.persenter.MyPreenter.get_mnemonic;
import static com.alm.token.ui.persenter.MyPreenter.pay;

public class SetActivity extends BaseActivity implements MainView<UpdateData>,
        ADBInstallApk.adbInstallAPKListener, MaterialDialog.SingleButtonCallback {
    @BindView(R.id.exitlogin_item)
    RelativeLayout exitloginItem;
    @BindView(R.id.managewallet_item)
    RelativeLayout managewalletItem;
    @BindView(R.id.changecurrency_item)
    RelativeLayout changecurrencyItem;
    @BindView(R.id.language_item)
    RelativeLayout languageItem;
    @BindView(R.id.contractaddress_item)
    RelativeLayout contractaddressItem;
    @BindView(R.id.g12costratio_item)
    RelativeLayout g12costratioItem;
    @BindView(R.id.appversion_item)
    RelativeLayout appversionItem;
    @BindView(R.id.tv_appversion)
    TextView tvAppversion;
    @BindView(R.id.walletmanger_item)
    RelativeLayout walletmangerItem;
    @BindView(R.id.email_item)
    RelativeLayout emailItem;
    @BindView(R.id.goolglecode_item)
    RelativeLayout goolglecodeItem;
    @BindView(R.id.changepasw_item)
    RelativeLayout changepaswItem;
    @BindView(R.id.changephone_item)
    RelativeLayout changephoneItem;
    @BindView(R.id.changepaypsw_item)
    RelativeLayout changepaypswItem;
    @BindView(R.id.isrelogin_item)
    RelativeLayout isrelogin_item;
    @BindView(R.id.monic_item)
    RelativeLayout monic_item;
    @BindView(R.id.switch_isrelogin)
    Switch switch_isrelogin;
    @BindView(R.id.tv_appversionshow)
    TextView tvAppversionshow;
    @BindView(R.id.tv_appversion_new)
    TextView tvAppversionNew;
    @BindView(R.id.tv_mail)
    TextView tv_mail;
    @BindView(R.id.tv_googlecode)
    TextView tv_googlecode;
    @BindView(R.id.tv_paypswd)
    TextView tv_paypswd;
    @BindView(R.id.tv_monic)
    TextView tv_monic;
    @BindView(R.id.tv_googlecode_show)
    TextView tv_googlecode_show;
    private int intentType = 0;

    @Override
    protected MyPreenter initPreenter() {
        return preenter = new MyPreenter(this);
    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_set;
    }

    @Override
    protected void initVariables() {
        intentType = getIntent().getIntExtra("SetActivity_type", 0);
    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(intentType != 0 ? R.string.mine_hint_item_safety : R.string.title_hint_item7));
        if (intentType != 0) {
            languageItem.setVisibility(View.GONE);
            contractaddressItem.setVisibility(View.GONE);
            appversionItem.setVisibility(View.GONE);
            exitloginItem.setVisibility(View.GONE);
            changephoneItem.setVisibility(View.GONE);
            isrelogin_item.setVisibility(View.VISIBLE);
            monic_item.setVisibility(View.VISIBLE);
            switch_isrelogin.setChecked(SpUtil.getBoolean(MyApp.context, ConStant.NEEDRELOGINANTTIME, false));
            isrelogin_item.setVisibility(View.GONE);

        } else {
            monic_item.setVisibility(View.GONE);
            walletmangerItem.setVisibility(View.GONE);
            emailItem.setVisibility(View.GONE);
            goolglecodeItem.setVisibility(View.GONE);
            changepaypswItem.setVisibility(View.GONE);
            changephoneItem.setVisibility(View.GONE);
            changepaswItem.setVisibility(View.GONE);
            isrelogin_item.setVisibility(View.GONE);
        }

        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            tvAppversion.setText(packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(SpUtil.getString(this, ConStant.UPDATEPATH, "")) && !MyApp.needUpdaate) {
            tvAppversionshow.setText(getString(R.string.set_hint_appversion));
            tvAppversionNew.setVisibility(View.GONE);
        } else {
            tvAppversionshow.setText(getString(R.string.set_hint_appversion1));
            tvAppversionNew.setVisibility(View.VISIBLE);
        }

        switch_isrelogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //控制开关字体颜色
                SpUtil.putBoolean(MyApp.context, ConStant.NEEDRELOGINANTTIME, b);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (EmptyUtils.isNotEmpty(MyApp.getUserData()) && EmptyUtils.isNotEmpty(MyApp.getUserData().getData())) {
            boolean bindMnemonic = MyApp.getUserData().getData().isBindMnemonic();
            boolean bindGoogleCode = MyApp.getUserData().getData().isBindGoogleCode();
            boolean payPwd = MyApp.getUserData().getData().isPayPwd();
            boolean bindEmail = MyApp.getUserData().getData().isBindEmail();

            tv_paypswd.setText(payPwd ? getString(R.string.set_hint_hadset) : getString(R.string.set_hint_noopen));
            tv_mail.setText(bindEmail ? "" : getString(R.string.set_hint_noopen));
            tv_googlecode.setText(bindGoogleCode ? getString(R.string.set_hint_hadopen) : getString(R.string.set_hint_noopen));
            tv_monic.setText(bindMnemonic ? "" : getString(R.string.set_hint_noopen));
            tv_googlecode_show.setText(getString(bindGoogleCode ? R.string.google_title_unbindgooglecode : R.string.group_hint_Googlecode));

            tv_paypswd.setTextColor(getResources().getColor(payPwd ? R.color.bg_653fac : R.color.text_666666));
            tv_mail.setTextColor(getResources().getColor(bindEmail ? R.color.bg_653fac : R.color.text_666666));
            tv_googlecode.setTextColor(getResources().getColor(bindGoogleCode ? R.color.bg_653fac : R.color.text_666666));
            tv_monic.setTextColor(getResources().getColor(bindMnemonic ? R.color.bg_653fac : R.color.text_666666));

            if (bindEmail && EmptyUtils.isNotEmpty(MyApp.getUserData().getData().getUser())) {
                UserData.DataBean.UserBean user = MyApp.getUserData().getData().getUser();
                tv_mail.setText(user.getEmail());
                if (user.getEmail() != null) {
                    String[] split = user.getEmail().split("@");
                    tv_mail.setText(split[0].length() > 4 ? split[0].substring(0, 2) + "****" + split[0].substring(split[0].length() - 2, split[0].length()) + "@" + split[1] : user.getEmail());
                }
            }
        }
    }

    @Override
    protected void loadData() {
        mLocalVersionCode = getVersionCode();
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 返回版本号
     *
     * @return 非-1 则代表获取成功
     */
    private int getVersionCode() {
        // 包管理者对象packageManager
        PackageManager pm = getPackageManager();
        // 从包的管理者对象中,获取指定包名的基本信息(版本名称,版本号),传0代表获取基本信息
        try {
            PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
            // 获取版本名称
            return packageInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    @OnClick({R.id.walletmanger_item, R.id.email_item, R.id.goolglecode_item, R.id.managewallet_item, R.id.changecurrency_item, R.id.language_item,
            R.id.contractaddress_item, R.id.g12costratio_item, R.id.appversion_item, R.id.monic_item,
            R.id.exitlogin_item, R.id.changepasw_item, R.id.changephone_item, R.id.changepaypsw_item, R.id.isrelogin_item})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.walletmanger_item:
                startActivity(WalletManagerActivity1.class);
                break;
            case R.id.email_item:
                if (EmptyUtils.isNotEmpty(MyApp.getUserData()) && EmptyUtils.isNotEmpty(MyApp.getUserData().getData())) {
                    boolean bindMnemonic = MyApp.getUserData().getData().isBindMnemonic();
                    boolean bindGoogleCode = MyApp.getUserData().getData().isBindGoogleCode();
                    boolean payPwd = MyApp.getUserData().getData().isPayPwd();
                    boolean bindEmail = MyApp.getUserData().getData().isBindEmail();
                    if (!bindEmail) {
                        DialogUtils.toBind(this, getSupportFragmentManager(), true, bindEmail, true, true);
                        break;
                    }
                }

                Intent intent = new Intent(SetActivity.this, BindEmaiActivity.class);
                intent.putExtra("BINDMAI_type", BINDMAI_up);
                startActivity(intent);
                break;
            case R.id.goolglecode_item:
                if (EmptyUtils.isNotEmpty(MyApp.getUserData()) && EmptyUtils.isNotEmpty(MyApp.getUserData().getData())) {
                    if (MyApp.getUserData().getData().isBindGoogleCode()) {

                        DialogUtils.enterGoogleCode(this, new DialogUtils.IEnterPwdListener() {
                            @Override
                            public void onSuccess(String pwd) {
                                preenter.unbindGoogleSecretKey(pwd);
                            }
                        });


//                        Intent intent1 = new Intent(SetActivity.this, BindGoogleCodeActivity.class);
//                        intent1.putExtra("BINDGOOGLECODE_type", BINDGOOGLECODE_UNBINd);
//                        startActivity(intent1);
                    } else {
                        Intent intent11 = new Intent(SetActivity.this, BindGoogleCodeActivity.class);
                        intent11.putExtra("BINDGOOGLECODE_type", BINDGOOGLECODE_BINd);
                        startActivity(intent11);
                    }
                }

                break;

            case R.id.changepasw_item:
//                showToast("修改密码click");

                if (EmptyUtils.isNotEmpty(MyApp.getUserData()) && EmptyUtils.isNotEmpty(MyApp.getUserData().getData())) {
                    boolean bindMnemonic = MyApp.getUserData().getData().isBindMnemonic();
                    boolean bindGoogleCode = MyApp.getUserData().getData().isBindGoogleCode();
                    boolean payPwd = MyApp.getUserData().getData().isPayPwd();
                    boolean bindEmail = MyApp.getUserData().getData().isBindEmail();
                    if (!bindEmail) {
                        DialogUtils.toBind(this, getSupportFragmentManager(), true, bindEmail, true, true);
                        break;
                    }
                }

                Intent intentfpsw = new Intent(this, FindPwdActivity.class);
                intentfpsw.putExtra("FindPwdActivity_type", FindPwdActivity.changeLoginPswtype);
                startActivity(intentfpsw);
                break;
            case R.id.changephone_item:
                startActivity(ChangePhoneActivity.class);
                break;
            case R.id.changepaypsw_item:
                if (EmptyUtils.isNotEmpty(MyApp.getUserData()) && EmptyUtils.isNotEmpty(MyApp.getUserData().getData())) {
                    boolean bindMnemonic = MyApp.getUserData().getData().isBindMnemonic();
                    boolean bindGoogleCode = MyApp.getUserData().getData().isBindGoogleCode();
                    boolean payPwd = MyApp.getUserData().getData().isPayPwd();
                    boolean bindEmail = MyApp.getUserData().getData().isBindEmail();
                    if (!bindEmail) {
                        DialogUtils.toBind(this, getSupportFragmentManager(), true, bindEmail, true, true);
                        break;
                    }
                }
                startActivity(PayPwdActivity.class);
                break;
            case R.id.managewallet_item:
//                showToast("管理钱包click");
                break;
            case R.id.changecurrency_item:
//                showToast("货币click");
                break;
            case R.id.language_item:
//                showToast("语言click");
                startActivity(SetLanguageActivity.class);
                break;
            case R.id.contractaddress_item:
                Intent intent2 = new Intent(SetActivity.this, SetLanguageActivity.class);
                intent2.putExtra("type", 1);
                startActivity(intent2);
//                showToast("合约版本click");
                break;
            case R.id.g12costratio_item:
//                showToast("G12费用比例click");
                break;
            case R.id.appversion_item:
                jumpToLoginAct(666);
//                preenter.getUpdateData("1");//1 安卓
                break;
            case R.id.exitlogin_item:
//                MyApp.userName = "";
//                WalletManager.getInstance().mListWallets = new ArrayList<>();
//                Intent intent3 = new Intent();
//                intent3.setAction(ConStant.EXIT_APP);
//                sendBroadcast(intent3);
//                startActivity(BlockWalletLoginActivity.class);
//                finish();
                break;
            case R.id.monic_item:

                if (EmptyUtils.isNotEmpty(MyApp.getUserData()) && EmptyUtils.isNotEmpty(MyApp.getUserData().getData())) {
                    boolean bindMnemonic = MyApp.getUserData().getData().isBindMnemonic();
                    boolean bindGoogleCode = MyApp.getUserData().getData().isBindGoogleCode();
                    boolean payPwd = MyApp.getUserData().getData().isPayPwd();
                    boolean bindEmail = MyApp.getUserData().getData().isBindEmail();
                    if (!payPwd || !bindEmail || !bindMnemonic) {
                        DialogUtils.toBind(this, getSupportFragmentManager(), payPwd, bindEmail, bindMnemonic, true);
                        break;
                    }
                }

                verify();
                break;
            case R.id.isrelogin_item:

                break;
        }
    }

    AlertDialog alert;

    private void verify() {
        alert = DialogUtils.showVerify(this, (pwd, code, google) -> {
            preenter.getMnemonic(code, pwd, google);
        }, code1 -> {
            if (MyApp.getUserData() != null && MyApp.getUserData().getData() != null && MyApp.getUserData().getData().getUser() != null) {
                String phone = MyApp.getUserData().getData().getUser().getPhone();
//                preenter.getSmsCodeData(phone, MyPreenter.pay, code1);
                preenter.getSmsCodeData(MyApp.getMail(), get_mnemonic);
            }
        }, flag -> {
            LogUtil.e("-------------to_TurnOutBiTBActivity");
            startActivityForResult(FundArealistActivity.class, INTENT_RESULT_INTO_CODE);
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserWalletEvent1(RefreshMeEvent refreshMeEvent) {
//        LogUtil.e("222---------------------");

        if (refreshMeEvent.index == 3) {
            if (EmptyUtils.isNotEmpty(MyApp.getUserData()) && EmptyUtils.isNotEmpty(MyApp.getUserData().getData())) {
                boolean bindMnemonic = MyApp.getUserData().getData().isBindMnemonic();
                boolean bindGoogleCode = MyApp.getUserData().getData().isBindGoogleCode();
                boolean payPwd = MyApp.getUserData().getData().isPayPwd();
                boolean bindEmail = MyApp.getUserData().getData().isBindEmail();

                tv_paypswd.setText(payPwd ? getString(R.string.set_hint_hadset) : getString(R.string.set_hint_noopen));
                tv_mail.setText(bindEmail ? "" : getString(R.string.set_hint_noopen));
                tv_googlecode.setText(bindGoogleCode ? getString(R.string.set_hint_hadopen) : getString(R.string.set_hint_noopen));
                tv_monic.setText(bindMnemonic ? "" : getString(R.string.set_hint_noopen));
                tv_googlecode_show.setText(getString(bindGoogleCode ? R.string.google_title_unbindgooglecode : R.string.group_hint_Googlecode));

                tv_paypswd.setTextColor(getResources().getColor(payPwd ? R.color.bg_653fac : R.color.text_666666));
                tv_mail.setTextColor(getResources().getColor(bindEmail ? R.color.bg_653fac : R.color.text_666666));
                tv_googlecode.setTextColor(getResources().getColor(bindGoogleCode ? R.color.bg_653fac : R.color.text_666666));
                tv_monic.setTextColor(getResources().getColor(bindMnemonic ? R.color.bg_653fac : R.color.text_666666));

                if (bindEmail && EmptyUtils.isNotEmpty(MyApp.getUserData().getData().getUser())) {
                    UserData.DataBean.UserBean user = MyApp.getUserData().getData().getUser();
                    tv_mail.setText(user.getEmail());
                    if (user.getEmail() != null) {
                        String[] split = user.getEmail().split("@");
                        tv_mail.setText(split[0].length() > 4 ? split[0].substring(0, 2) + "****" + split[0].substring(split[0].length() - 2, split[0].length()) + "@" + split[1] : user.getEmail());
                    }
                }
            }
        }
    }


    @Override
    public void getData(UpdateData updateData) {
        if (updateData.getStatus() == 0) {//请求成功

//            UpdateData.DataBean dataBean = updateData.getData();
//            updateApkUrl = dataBean.getDownload();
//            MyApp.setUpdateApkUrl(updateApkUrl);
////            updateApkUrl = "http://gyxz.ukdj3d.cn/a3/rj_wn1/360daikuandaohang.apk";//模拟下载安装地址
//            apkname = dataBean.getApkname() + dataBean.getVersioncode();
////            showToast("apkname=" + dataBean.getApkname() + " url=" + updateApkUrl + " versioncode=" + dataBean.getVersioncode());
//            if (Integer.parseInt(dataBean.getVersioncode()) > mLocalVersionCode) {
////                showUpdateAlertDialog(this, "版本更新", "有新版本更新啦!", "同意", "取消", this);
//                if (SpUtil.getString(this, ConStant.UPDATEPATH, "").isEmpty()
//                        || (!SpUtil.getString(this, ConStant.UPDATEPATH, "").isEmpty()
//                        && SpUtil.getString(this, ConStant.UPDATEPATH, "").indexOf(apkname) == -1)) {
//                    showUpdateAlertDialog(this, "版本更新", "有新版本更新啦!", "同意", "取消", this);
//                } else {
//                    LogUtil.d("apkpath" + SpUtil.getString(this, ConStant.UPDATEPATH, ""));
//                    onDownloadSuccess(SpUtil.getString(this, ConStant.UPDATEPATH, ""));
//                }
//            } else {
//                SpUtil.putString(this, ConStant.UPDATEPATH, "");
//            }
        }
    }

    @Override
    public void getCode(RegisterData registerData) {
        showToast(registerData.getMsg());
    }

    @Override
    public void getMonic(GoogleAuthKeyData googleAuthKeyData) {


        if (googleAuthKeyData.getStatus() == 0 && googleAuthKeyData.getData() != null) {
            Intent intent = new Intent(SetActivity.this, ExportMnemonicActivity.class);
            intent.putExtra("ExportMnemonic", googleAuthKeyData.getData());
            startActivity(intent);

//            UserWalletData.DataBean.SysMsgBean sysMsgBean1 = new UserWalletData.DataBean.SysMsgBean();
//            sysMsgBean1.setTitle(getString(R.string.wallet_import_hint13));
//            sysMsgBean1.setContent(googleAuthKeyData.getData());
//            sysMsgBean1.setUpStartTimeStr(getString(R.string.common_close));
//            sysMsgBean1.setUpEndTimeStr(getString(R.string.wallet_import_hint19));
//            DialogUtils.showInforsetLang(this, sysMsgBean1, (pwd) -> {
//                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//                cm.setPrimaryClip(ClipData.newPlainText(null, googleAuthKeyData.getData()));
//                showToast(getString(R.string.profile_hint_item26));
//            });
        } else {
            showToast(googleAuthKeyData.getMsg());
        }
        if (EmptyUtils.isNotEmpty(alert) && alert.isShowing()) {
            alert.dismiss();
        }

    }

    @Override
    public void unbindGoogleSecretKey(GoogleAuthKeyData googleAuthKeyData) {
        showToast(googleAuthKeyData.getMsg());
        if (googleAuthKeyData.getStatus() == 0) {
            EventBus.getDefault().post(new RefreshMeEvent(1));
            finish();
        }

    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this,PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
            showToSetPaypswDialog();
        }else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {

        hideWaitDialog();
    }

    @Override
    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
        switch (which) {
            case NEUTRAL:

                break;
            case NEGATIVE:

                hideUpdateAlertDialog();
                break;
            case POSITIVE:
                if (dialog.getTitleView().getText().toString().equals("版本更新")) {
                    downloadApk();
                    progressDialog = showProgressDialog(this, "", new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

                        }
                    });
                    if (progressDialog != null) {
                        progressDialog.setProgress(0);
                    }

                } else if (dialog.getTitleView().getText().toString().equals("新版本安装")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    File file = new File(SpUtil.getString(this, ConStant.UPDATEPATH, ""));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        Uri contentUri = FileProvider.getUriForFile(this, getApplication().getPackageName() + ".fileProvider", file);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
                    } else {
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                    }
                    startActivity(intent);
                }
                break;
        }

    }


}
